package com.linghui.videoplus.ipai.activity;

import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.UnderlineSpan;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.linghui.videoplus.ipai.R;
import com.linghui.videoplus.ipai.VideoPlusApplication;
import com.linghui.videoplus.ipai.api.json.JsonAPI;
import com.linghui.videoplus.ipai.bean.NickNameBean;
import com.linghui.videoplus.ipai.utils.Constants;
import com.linghui.videoplus.ipai.utils.StringUtil;

public class InitActivity extends BaseActivity implements OnClickListener,
		OnCheckedChangeListener {

	private ViewFlipper accountViewFlipper;

	private EditText nickNameEidtText;
	private RadioButton maleRadioButton;
	private RadioButton femaleRadioButton;
	private RadioGroup sexRadioGroup;
	private TextView toBrowserTextView;
	private Animation slideLeftIn;
	private Animation slideLeftOut;
	private float startX;
	private String sex;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_init);
		accountViewFlipper = (ViewFlipper) findViewById(R.id.accountViewFlipper);
		accountViewFlipper.setDisplayedChild(0);
		slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
		slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
		toBrowserTextView = (TextView) findViewById(R.id.toBrowserTextView);
		SpannableString msp = new SpannableString("开始探索");
		msp.setSpan(new UnderlineSpan(), 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
		toBrowserTextView.setText(msp);
		toBrowserTextView.setOnClickListener(this);


		nickNameEidtText = (EditText) findViewById(R.id.nickNameEditText);
		maleRadioButton = (RadioButton) findViewById(R.id.maleRadioButton);
		femaleRadioButton = (RadioButton) findViewById(R.id.femaleRadioButton);
		sexRadioGroup = (RadioGroup) findViewById(R.id.sexRadiogroup);
		sexRadioGroup.setOnCheckedChangeListener(this);

	}

	// @Override
	// public boolean onTouchEvent(MotionEvent event) {
	// switch (event.getAction()) {
	// case MotionEvent.ACTION_DOWN:
	// startX = event.getX();
	// break;
	// case MotionEvent.ACTION_UP:
	// if (event.getX() - startX < -60) { // 向左滑动
	// if (accountViewFlipper.getDisplayedChild() == 1) {
	// return false;
	// }
	// accountViewFlipper.setInAnimation(slideLeftIn);
	// accountViewFlipper.setOutAnimation(slideLeftOut);
	// accountViewFlipper.showNext();
	// }
	// break;
	// }
	// return super.onTouchEvent(event);
	// }

	@Override
	public void onClick(View v) {
		if (v == toBrowserTextView) {
			String nickName = nickNameEidtText.getText().toString();
			if (checkNickName(nickName, sex)) {
				NickNameBean nick = new NickNameBean();
				nick.setUuid(VideoPlusApplication.getDeviceUUID(this));
				nick.setNickName(nickName);
				nick.setSex(sex);
				JsonAPI jsonApi = new JsonAPI();
				jsonApi.saveNickName(this, nick);
				BrowserActivity.launch(this,VideoPlusApplication.getUrlIndex(this), REQUEST_CODE_INIT, REQUEST_CODE_DEFAULT);
			}
		}
	}

	@Override
	public void onCheckedChanged(RadioGroup arg0, int arg1) {
		if (arg1 == maleRadioButton.getId()) {
			sex = NickNameBean.SEX_MALE;
		} else if (arg1 == femaleRadioButton.getId()) {
			sex = NickNameBean.SEX_FEMALE;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			VideoPlusApplication.exitApplication(this);
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * 判断
	 * 
	 * @param nickName
	 * @param male
	 * @param female
	 * @return
	 */
	private boolean checkNickName(final String nickName, final String sex) {
		if (StringUtil.isEmpty(nickName)) {
			showToast("请输入昵称!");
			return false;
		}
		
		if(nickName.length()>20 || nickName.length()<4){
			showToast("昵称仅支持4到20个字符!");
			return false;
		}
		if (StringUtil.isEmpty(sex)) {
			showToast("请选择性别!");
			return false;
		}
		return true;
	}

}
